package com.zr.controller;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.zr.model.Student;
import com.zr.model.Teacher;
import com.zr.model.User;
import com.zr.service.StudentServiceHyx;
import com.zr.service.UserServiceHyx;
import com.zr.serviceImpl.StudentServiceImplHyx;
import com.zr.serviceImpl.UserServiceImplHyx;

public class AddTeacherStudentControllerHyx extends HttpServlet{

	StudentServiceHyx sService = new StudentServiceImplHyx();
	UserServiceHyx uService = new UserServiceImplHyx();
	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		doPost(req, resp);
	}
	@Override
	protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		req.setCharacterEncoding("UTF-8");
		resp.setCharacterEncoding("UTF-8");
		HttpSession session = req.getSession();
		Teacher t = (Teacher) session.getAttribute("curtTeacher");
		
		String uname = req.getParameter("uname");
		String upsw = req.getParameter("upsw");
		String sname = req.getParameter("sname");
		String sage = req.getParameter("sage");
		String ssex = req.getParameter("ssex");
		String saddress = req.getParameter("saddress");
		String spnumber = req.getParameter("spnumber");	
		String stime = req.getParameter("stime");
		String bname = req.getParameter("banji");
		
		//添加学生
		boolean flag1 = sService.adminAddStudent(sname, sage, ssex, saddress, spnumber, stime, bname);
		//添加学生用户
		int rid = 3;
		boolean flag2 = uService.addUserForNewStudent(uname, upsw, rid);
		//为新学生匹配用户
		Student s = sService.findNewStudent(sname, sage, ssex, saddress, spnumber, stime, bname);
		User user = uService.findNewUserForStudent(uname,upsw);
		boolean flag3 = uService.updateUserTidForNewStudent(user.getUid(), s.getSid());
		//匹配当前老师
		boolean flag4 = sService.adminInsertS_T(s.getSid(), t.getTid());
		
		PrintWriter pw = resp.getWriter();		
		if(flag1&&flag2&&flag3&&flag4){
				pw.write("1");
			}else{
				pw.write("0");
		}
		pw.flush();
		pw.close();
	}

		
	
}
